package com.dfrz.dao;

import com.dfrz.entry.BusinessInfo;
import com.dfrz.entry.User;
import com.dfrz.util.JdbcUtils;
import com.mysql.cj.util.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

//商家登录
public class BusinessInfoDao {
    public BusinessInfo buslogin(String loginname,String password){
        BusinessInfo businessInfo=null;
        String sqlStr="select * from res_businessinfo where loginname=? and password=?";
        QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
        try {
            businessInfo=queryRunner.query(sqlStr,new BeanHandler<BusinessInfo>(BusinessInfo.class),loginname,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return businessInfo;
    }
    public int insertBusiness(BusinessInfo businessInfo){
        int count=0;
        String sqlStr="insert into res_businessinfo(loginname,password,storename,logoURL,address,phone,status,name,headerURL) values(?,?,?,?,?,?,?,?,?)";
        QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
        try {
            count=queryRunner.update(sqlStr,businessInfo.getLoginname(),
                    businessInfo.getPassword(),
                    businessInfo.getStorename(),
                    businessInfo.getLogoURL(),
                    businessInfo.getAddress(),
                    businessInfo.getPhone(),
                    businessInfo.getStatus(),
                    businessInfo.getName(),
                    businessInfo.getHeaderURL()
                    );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;

    }//更新
    public int undateBusinessInfo(BusinessInfo businessInfo){
        int count=0;
        QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
        String sqlStr="update res_businessinfo set id="+businessInfo.getId();
        if (businessInfo.getLoginname()!=null||!"".equals(businessInfo.getLoginname())){
             sqlStr+=",loginname="+businessInfo.getLoginname();
        }
        if (businessInfo.getPassword()!=null||!"".equals(businessInfo.getPassword())){
            sqlStr+=",password="+businessInfo.getPassword();
        }
        if (businessInfo.getStorename()!=null||!"".equals(businessInfo.getStorename())){
            sqlStr+=",storename="+businessInfo.getStorename();
        }
        if (businessInfo.getLogoURL()!=null||!"".equals(businessInfo.getLogoURL())){
            sqlStr+=",logoURL="+businessInfo.getLogoURL();
        }
        if (businessInfo.getAddress()!=null||!"".equals(businessInfo.getAddress())){
            sqlStr+=",address="+businessInfo.getAddress();
        }
        if (businessInfo.getPhone()!=null||!"".equals(businessInfo.getPhone())){
            sqlStr+=",phone="+businessInfo.getPhone();
        }
        if (businessInfo.getName()!=null||!"".equals(businessInfo.getName())){
            sqlStr+=",name="+businessInfo.getName();
        }
        if (businessInfo.getHeaderURL()!=null||!"".equals(businessInfo.getHeaderURL())){
            sqlStr+=",headerURL="+businessInfo.getHeaderURL();
        }
        sqlStr+="where id="+businessInfo.getId();
        try {
            count=queryRunner.update(sqlStr);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;

    }
    //逻辑删除
    public int deleteBusiness(Integer id){
        int count=0;
        String sqlStr="update res_businessinfo set status=-1 where id=?";
        QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
        try {
            count=queryRunner.update(sqlStr,id);
        } catch (SQLException e) {

        }
        return count;
    }
    public BusinessInfo queryBusinessByType(String val, Integer type){
      BusinessInfo businessInfo =null;
        String sqlStr="";
        if (type==1){
            sqlStr="select * from res_businessinfo where loginname=?";
        }else if (type==2){
            sqlStr="select *from res_businessinfo where phone=?";
        }else{
            sqlStr="select * from res_businessinfo where eMail=?";
        }
        QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
        try {
            businessInfo = queryRunner.query(sqlStr,new BeanHandler<BusinessInfo>(BusinessInfo.class),val);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return businessInfo;
    }
    //分页查询
    public List<BusinessInfo>queryBusinessByPage(String name,int limit,int starNum){
        List<BusinessInfo>listBus=null;
        String sqlStr=null;
        if (!StringUtils.isNullOrEmpty(name)){
            sqlStr="select * from res_businessinfo where name like'?' and status=1 order by id desc limit ?,?";
            QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
            try {
                listBus=queryRunner.query(sqlStr,new BeanListHandler<BusinessInfo>(BusinessInfo.class),name,starNum,limit);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        else {
            sqlStr="select * from res_businessinfo where status=1 orderd by id desc limit ?,?";
            QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
            try {
                listBus=queryRunner.query(sqlStr,new BeanListHandler<BusinessInfo>(BusinessInfo.class),starNum,limit);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return listBus;
    }
    public int queryCount(String name){
        long countL=0;
        String sqlStr=null;
        if (!StringUtils.isNullOrEmpty(name)){
            sqlStr="select COUNT(1) from res_businessinfo where name like '?' and status=1";
            QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
            try {
                countL=queryRunner.query(sqlStr,new ScalarHandler<>(),name);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else {
            sqlStr="select COUNT(1) from res_businessinfo where status=1";
            QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
            try {
                countL=queryRunner.query(sqlStr,new ScalarHandler<>());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return (int) countL;
    }

    public static void main(String[] args) {
        BusinessInfoDao businessInfoDao=new BusinessInfoDao();
        BusinessInfo businessInfo=businessInfoDao.buslogin("111","1111");
        System.out.println(businessInfo);
    }

}
